<template>
  <div class="table">
    <div class="crumbs">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>
          <i class="el-icon-lx-cascades"></i>视频管理
        </el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="container">
      <div class="handle-box">
       <el-select v-model="select_status" placeholder="筛选状态" class="handle-select mr10" @change="selectUserStatus">
          <el-option key="1" label="启用" value="0"></el-option>
            <el-option key="2" label="禁用" value="1"></el-option>
              <el-option key="3" label="全部" value="-1"></el-option>
        </el-select>
        <el-input v-model="select_word" placeholder="筛选关键词" class="handle-input mr10"></el-input>
        <el-button type="primary" icon="el-icon-search" @click="search">搜索</el-button>
        <el-button type="primary" icon="el-icon-lx-add" @click="addVisible = true">添加视频</el-button>
      </div>
      <el-table
        :data="data.slice((currentPage-1)*pagesize,currentPage*pagesize)"
        border
        class="table"
        ref="multipleTable"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="index" width="80" align="center" sortable label="序列"></el-table-column>
        <el-table-column prop="vname" label="视频名称" width="180"></el-table-column>
        <el-table-column prop label="视频" width="100">
          <template slot-scope="scope">
            <a
              target="_blank"
              :href="`http://localhost/fitness/public/static/video/`+scope.row.address"
            >点击查看</a>
          </template>
        </el-table-column>
        <el-table-column prop="tname" label="视频类型" width="80"></el-table-column>
        
        <el-table-column prop="intro" label="视频简介" width="auto"></el-table-column>
        <el-table-column prop="vdate" label="上传时间" sortable width="200"></el-table-column>
        <el-table-column prop="clubname" label="所属俱乐部" width="180"></el-table-column>
        <el-table-column prop="cstatus" label="课程状态" width="100" align="center">
          <template slot-scope="scope">
            <template v-if="scope.row.vstatus===0">
              <i class="el-icon-circle-check" style="color:#1e90ff">已启用</i>
            </template>
            <template v-else>
              <i class="el-icon-circle-close" style="color:red">已禁用</i>
            </template>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="200" align="center">
          <template slot-scope="scope">
            <template v-if="scope.row.vstatus===0">
              <el-button
                type="text"
                icon="el-icon-delete"
                class="red"
                @click="handleBan(scope.row)"
              >禁用</el-button>
            </template>
            <template v-else>
              <el-button type="text" icon="el-icon-circle-check" @click="handleOn(scope.row)">启用</el-button>
              <el-button type="text" icon="el-icon-edit" @click="onclickEdit(scope.row)">修改</el-button>
            </template>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[5, 8, 20, 40]"
          :page-size="pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="videoData.length"
        ></el-pagination>
      </div>
    </div>

    <!-- 添加视频弹出框 -->
    <el-dialog title="请填写视频信息" :visible.sync="addVisible" width="30%">
      <el-form ref="addform" :model="addform" :rules="rules" label-width="80px">
        <el-form-item label="视频名称" prop="vname">
          <el-input v-model="addform.vname"></el-input>
        </el-form-item>
        <el-form-item label="上传视频">
          <el-upload
            ref="upload"
            class="upload-demo"
            action="/api/Video/addvideo"
            name="video"
            :file-list="fileList"
            :on-change="handleChange"
            :on-remove="handleRemove"
            :limit="1"
            list-type="mp4"
            :on-success="handlePicsuccess"
            accept=".mp4"
            :auto-upload="false"
            :data="addform"
          >
            <el-button size="small" type="primary" @click="videolimit">点击上传</el-button>
            <div slot="tip" class="el-upload__tip">请保证视频格式正确，且不超过10M</div>
          </el-upload>
        </el-form-item>
        <el-form-item label="视频类型" prop="tid">
          <el-select v-model="addform.tid" placeholder="请选择">
            <el-option
              v-for="item in videotype"
              :key="item.tid"
              :label="item.tname"
              :value="item.tid"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item hidden label="属俱乐部">
          <el-input v-model="clubid"></el-input>
        </el-form-item>
        <el-form-item label="视频简介" prop="intro">
          <el-input v-model="addform.intro"></el-input>
        </el-form-item>
        <el-form-item label="上传时间" prop="vdate">
          <el-date-picker v-model="addform.vdate" type="datetime" value-format="yyyy-MM-dd HH:mm:ss" placeholder="选择日期"></el-date-picker>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveAdd">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 编辑弹出框 -->
    <el-dialog title="修改视频" :visible.sync="editVisible" width="30%">
      <el-form ref="form" :model="form" :rules="rules1" label-width="80px">
        <el-form-item label="视频名称" prop="vname">
          <el-input v-model="form.vname"></el-input>
        </el-form-item>
        <el-form-item label="上传视频">
          <el-upload
            ref="upload"
            class="upload-demo"
            action="/api/Video/editvideo"
            name="video"
            :file-list="fileListedit"
            :limit="1"
            list-type="mp4"
            accept=".mp4"
            :on-success="handlePicsuccess"
            :auto-upload="false"
            :data="form"
          >
            <el-button size="small" type="primary" @click="videolimit">点击上传</el-button>
            <div slot="tip" class="el-upload__tip">请保证视频格式正确，且不超过10M</div>
          </el-upload>
          <a target="_blank" :href="videoUrl">点击查看原视频</a>
        </el-form-item>
        <el-form-item label="视频类型" prop="tid">
          <el-select v-model="form.tid" placeholder="请选择">
            <el-option
              v-for="item in videotype"
              :key="item.tid"
              :label="item.tname"
              :value="item.tid"
            ></el-option>
          </el-select>
          <!-- <p>原视频类型:{{form.tname}}</p> -->
        </el-form-item>
        <!-- <el-form-item hidden label="属俱乐部" prop="clubid">
          <el-input v-model="clubid"></el-input>
          <el-select v-model="form.clubid" placeholder="请选择">
            <el-option
              v-for="item in videoclub"
              :key="item.clubid"
              :label="item.clubname"
              :value="item.clubid"
            ></el-option>
          </el-select>
        </el-form-item> -->
        <el-form-item label="视频简介" prop="intro">
          <el-input v-model="form.intro"></el-input>
        </el-form-item>
        <el-form-item label="上传时间" prop="vdate">
          <el-date-picker v-model="form.vdate" type="datetime" value-format="yyyy-MM-dd HH:mm:ss" placeholder="选择日期"></el-date-picker>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveEdit">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 禁用提示框 -->
    <el-dialog title="提示" :visible.sync="banVisible" width="300px" center>
      <div class="del-dialog-cnt">确定禁用该视频？</div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="banVisible = false">取 消</el-button>
        <el-button type="primary" @click="confirmBan">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 启用提示框 -->
    <el-dialog title="提示" :visible.sync="onVisible" width="300px" center>
      <div class="del-dialog-cnt">确定发布该视频？</div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="onVisible = false">取 消</el-button>
        <el-button type="primary" @click="confirmOn">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { log } from "util";
export default {
  name: "basetable",
  data() {
    return {
      currentPage: 1, //初始页
      pagesize: 5, //    每页的数据
      select_status: '全部',
      multipleSelection: [],
      select_cate: "",
      select_word: "",
      is_search: false,
      addVisible: false,
      editVisible: false,
      banVisible: false,
      onVisible: false,
      videoData: [],
      videotype: [],
      videoclub: [],
      videoUrl: "",
      fileList: [],
      fileListedit: [],
      addform: {
        vname: "",
        vdate: "",
        intro: "",
        tid: "",
        tname: "",
        clubid: localStorage.clubid,
        clubname: ""
      },
      form: {
        vname: "",
        vdate: "",
        intro: "",
        tid: "",
        tname: "",
        clubname: "",
        Vid: ""
      },
      rules: {
        vname: [
          {
            required: true,
            message: "视频名称不能为空",
            trigger: "blur"
          }
        ],
        tid: [
          {
            required: true,
            message: "请选择视频类型",
            trigger: "change"
          }
        ],
        clubid: [
          {
            required: true,
            message: "请选择所属俱乐部",
            trigger: "change"
          }
        ],
        intro: [
          {
            required: true,
            message: "视频简介不能为空",
            trigger: "blur"
          }
        ],
        vdate: [
          {
            type: "string",
            required: true,
            message: "请选择上传时间",
            trigger: "blur"
          }
        ]
      },
      rules1: {
        vname: [
          {
            required: true,
            message: "视频名称不能为空",
            trigger: "blur"
          }
        ],
        tid: [
          {
            required: true,
            message: "请选择视频类型",
            trigger: "change"
          }
        ],
        clubid: [
          {
            required: true,
            message: "请选择所属俱乐部",
            trigger: "change"
          }
        ],
        intro: [
          {
            required: true,
            message: "视频简介不能为空",
            trigger: "blur"
          }
        ],
        vdate: [
          {
            type: "string",
            required: true,
            message: "请选择上传时间",
            trigger: "blur"
          }
        ]
      },
      banVid: 0,
      onVid: 0,
      editVisible: false,
      // page
      pageNum: 1,
      pageSize: 5,
      dataSize: 0,
      clubid: localStorage.clubid
    };
  },
  created() {
    this.getData();
    this.getvideotype();
    this.getvideoclub();
  },
  computed: {
    data() {
      return this.videoData.filter(d => {
        if (
          d.vname.indexOf(this.select_word) > -1 ||
          d.tname.indexOf(this.select_word) > -1
        ) {
          return d;
        }
      });
    }
  },
  watch: {
    pageNum: function() {
      this.getData();
    },
    pageSize: function() {
      this.getData();
    }
  },
  methods: {
     selectUserStatus() {
      if (this.select_status == -1) {
        this.getData();
      } else {
        this.getStatusData(this.select_status);
      }
      console.log('object :', this.videoData);
    },
    videolimit(){
    this.$refs.upload.clearFiles();
    },
    handleSizeChange: function(size) {
      this.pagesize = size;
      // console.log(this.pagesize); //每页下拉显示数据
    },
    handleCurrentChange: function(currentPage) {
      this.currentPage = currentPage;
      // console.log(this.currentPage); //点击第几页
    },

    //分页
    handleRemove(file, fileList) {
      // console.log(file, fileList);
    },
    handleChange(file, fileList) {
      // console.log("777", file, fileList);
      this.fileList = fileList;
    },
    onclickEdit(row) {
      this.editVisible = true;
      console.log("row=", row);
      this.form = JSON.parse(JSON.stringify(row));
      // console.log("this.form=", this.form);
      this.videoUrl =
        "http://localhost/fitness/public/static/video/" + row.address;
    },
     getStatusData(status){
        this.$axios.get('/api/Video/videoliststatus',{
        params: {
            clubid: localStorage.clubid,
            status: status,
       }
      }).then(res => {
        this.videoData = res.data;
      })
     },
    getData() {
      this.$axios.get("/api/Video/videolist",{
        params: {
          clubid: localStorage.clubid
        }
      }).then(response => {
        // console.log("response :", response.data);
        this.videoData = response.data;
      });
    },
    getvideotype() {
      this.$axios.get("/api/Video/videotype").then(response => {
        // console.log("response :", response.data);
        this.videotype = response.data;
      });
    },
    getvideoclub() {
      this.$axios.get("/api/Video/videoclub").then(response => {
        // console.log("response :", response.data);
        this.videoclub = response.data;
      });
    },
    search() {
      this.is_search = true;
    },
    filterTag(value, row) {
      return row.tag === value;
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    saveEdit() {
      let vm = this;
      this.$refs["form"].validate(valid => {
        if (valid) {
          vm.$axios.post("/api/Video/editvideo", this.form).then(res => {
            console.log("res :", res.data);
          });
          vm.$refs.upload.submit();
          this.$message.success("修改成功");
          this.getData();
          this.editVisible = false;
        } else {
          this.$message.error("修改失败,请填写必填或正确信息");
          this.editVisible = true;
        }
      });
    },
    saveAdd() {
      let vm = this;
      this.$refs["addform"].validate(valid => {
        if (valid) {
          console.log(this.fileList);
          if (!(this.fileList && this.fileList.length > 0)) {
            console.log(this.fileList);
            this.$message.error("添加失败,请添加图片");
            return;
          }
          vm.$refs.upload.submit();
          this.$message.success("添加成功");
          this.getData();
          this.addVisible = false;
        } else {
          this.$message.error("添加失败,请填写必填信息");
          this.addVisible = true;
        }
      });
      // this.getData();
    },
    confirmBan() {
      this.$axios
        .get("/api/Video/banvideo", {
          params: {
            vid: this.banVid
          }
        })
        .then(res => {
          console.log("res :", res.data);
          if (res.data.flag == 1) {
            this.$message.error("禁用成功");
            this.getData();
          }
        });
      this.banVisible = false;
    },
    confirmOn() {
      this.$axios
        .get("/api/Video/onvideo", {
          params: {
            vid: this.onVid
          }
        })
        .then(res => {
          console.log("res :", res.data);
          if (res.data.flag == 1) {
            this.$message.success("启用成功");
            this.getData();
          }
        });
      this.onVisible = false;
    },
    handleBan(row) {
      this.banVisible = true;
      this.banVid = row.vid;
    },
    handleOn(row) {
      this.onVisible = true;
      // console.log(row);
      this.onVid = row.vid;
    },
    editvisible(vid) {
      this.editVisible = true;
      this.form.Vid = vid;
    },
    handlePicsuccess(response) {
      console.log(response);
    }
  }
};
</script>

<style scoped>
p {
  color: #ff0000;
}
a {
  color: #ff0000;
}
.handle-box {
  margin-bottom: 20px;
}

.handle-select {
  width: 120px;
}

.handle-input {
  width: 300px;
  display: inline-block;
}
.del-dialog-cnt {
  font-size: 16px;
  text-align: center;
}
.table {
  width: 100%;
  font-size: 14px;
}
.red {
  color: #ff0000;
}
.mr10 {
  margin-right: 10px;
}
</style>
